/**
 * Created by henian.xu on 2018/9/18.
 *
 */

import {makeAbsolute, normalizePath} from './util';
import {loadCompiledFiles} from './load';
import {parseFiles} from './process';

/**
 * Get rendered stats required for extraction
 */
function getRenderedStats(rendered, compileOptions) {
  return {
    entryFilename: normalizePath(rendered.stats.entry),
    includedFiles: rendered.stats.includedFiles.map(f => normalizePath(makeAbsolute(f))),
    includedPaths: (compileOptions.includePaths || []).map(normalizePath),
  };
}

export function extract(rendered, {compileOptions = {}, extractOptions = {}} = {}) {

  const {
    entryFilename,
    includedFiles,
    includedPaths,
  } = getRenderedStats(rendered, compileOptions);
  // console.log(getRenderedStats(rendered, compileOptions));
  return loadCompiledFiles(includedFiles, entryFilename, compileOptions.data).then(({compiledFiles, orderedFiles}) => {
    const parsedDeclarations = parseFiles(compiledFiles);
    console.log(compiledFiles,parsedDeclarations)
  });
}